package coc.core.collector;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import coc.core.url.Url;
import coo.base.exception.UncheckedException;

/**
 * 基于Jsoup的采集器。
 */
public class JsoupCollector implements Collector {
	private Connection connection;
	private Document document;

	/**
	 * 构造方法。
	 * 
	 * @param conTimeout
	 *            超时时间
	 */
	public JsoupCollector(Integer conTimeout) {
		connection = Jsoup.connect("http://no.this.url").userAgent("Mozilla")
				.timeout(conTimeout * 1000);
	}

	@Override
	public void collect(Url url) {
		try {
			document = connection.url(url.toString()).get();
		} catch (Exception e) {
			throw new UncheckedException("加载网页时发生异常:" + e.getMessage() + "["
					+ url + "]");
		}
	}

	@Override
	public void setTimeout(Integer timeout) {
		connection.timeout(timeout * 1000);
	}

	@Override
	public Document createDocument() {
		Elements baseElements = document.select("base[href]");
		if (!baseElements.isEmpty()) {
			String baseUri = baseElements.first().attr("href");
			document.setBaseUri(baseUri);
		}
		return document;
	}

	@Override
	public void destroy() {
		connection = null;
	}

	public Connection getConnection() {
		return connection;
	}
}
